//
// Created by isabella on 25-5-27.
//
#include <common.h>

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int> > &prerequisites) {
        unordered_map<int, vector<int> > data_set(numCourses);
        vector<int> degree(numCourses);
        for (int i = 0; i < prerequisites.size(); i++) {
            int from = prerequisites[i][1];
            int to = prerequisites[i][0];
            data_set[from].push_back(to);
            degree[to]++;
        }
        vector<int> sequence;
        for (int i = 0; i < numCourses; i++) {
            int src = -1;
            for (int i = 0; i < degree.size(); i++) {
                if (degree[i] == 0) {
                    src = i;
                }
            }
            if (src == -1) {
                sequence.clear();
                return sequence;
            }
            sequence.push_back(src);
            degree[src]--;
            auto tmp_arr = data_set[src];
            for (int arr: tmp_arr) {
                degree[arr]--;
            }
        }
        return sequence;
    }
};

int main() {
    auto sp = new Solution();
    vector<vector<int> > prerequisites = {{1, 0}};
    sp->findOrder(2, prerequisites);
    delete sp;
    return 0;
}
